/**
 * 
 * @authors Your Name (you@example.org)
 * @date    2018-08-23 09:08:43
 * @version $Id$
 */
function fn() {
	//获取后端的数据；
        var xhr = new XMLHttpRequest();
        xhr.open("post", "http://www.blogzl.com/zl_other_module/w1804/api/login.php");
        xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
        var user = document.getElementsByName("username")[0].value;
        var pass = document.getElementsByName("password")[0].value;
        var mes = { "username": "张三", "password": "password" };
        //创建对象，将输入的值赋值给对象
        mes.username = user;
        mes.password = pass;
        //判断密码是否为6-12位的字符串
        var password=pass.match(/\w{6,12}/);
        if(password==null){
        	alert("请输入6-12位的密码【数字，字母或下划线】");
        	return false;
        }
        xhr.send("mes=" + JSON.stringify(mes));
        xhr.onreadystatechange = function() {
        	//当获取数据成功之后，执行先一下操作
            if (xhr.readyState == 4 && xhr.status == 200) {
                var v = xhr.responseText;
                v = JSON.parse(v);
                console.info(v);
                //判断拉取得数据是否为成功得请求
                if (v.state == "success") {
                	//成功就再cookie里设置相应的值判断是否登陆成功
                    var date = new Date();
                    var ms = date.getTime() + 30 * 24 * 3600 * 1000;
                    date.setTime(ms);
                    var utc = date.toUTCString();
                    document.cookie = "state=yes; path=/;expires=" + utc;
                    location.replace("index.html");
                    console.info(document.cookie);
                } else {
                    alert(v.des);
                    return false;
                }
            }
        }
    }
